/**
 * Created by wanghuan on 2016/11/15.
 */

const API_ROOT = ''
import { Message } from 'element-ui';
export const  apiPost = function (url,commit,option,success) {
  option = option || {};
  $.ajax({
    url:API_ROOT+url,
    data:JSON.stringify(option.param),
    dataType:'json',
    type:'post',
    contentType: "application/json;charset=UTF-8",
    beforeSend:function(){
     //commit('setGlobalLoading',true);
    },
    success: function (data) {
      toLogin(data);
      if (data.result == "true"){
        if (option.success && typeof option.success == 'function'){
          option.success(data,option.extra)
        }
        if (success && typeof success == 'function'){
          success(data);
        }
      }else{
        if (data.resultDesc){
          Message.error(data.resultDesc);
        }
      }
    },
    error: function () {
      Message.error('request error');
    },
    complete:function(){
     //commit('setGlobalLoading',false);
    }
  })
};

export const  apiGet = function (url,commit,option,success) {
  option = option || {};
  $.ajax({
    url:API_ROOT+url,
    data:option.param,
    dataType:'json',
    type:'post',
    beforeSend:function(){
      commit('setGlobalLoading',true);
    },
    success: function (data) {
      toLogin(data);
      if (data.result == "true"){
        if (option.success && typeof option.success == 'function'){
          option.success(data,option.extra)
        }
        if (success && typeof success == 'function'){
          success(data);
        }
      }else{
        if (data.resultDesc){
          Message({
            message:data.resultDesc,
            type:'error'
          })
        }
      }
    },
    error: function () {
      Message({
        message:'error',
        type:'error'
      })
    },
    complete:function(){
      commit('setGlobalLoading',false);
    }
  })
};

export const  apiUpload = function (url,commit,option,success) {
  option = option || {};
  $.ajax({
    url:API_ROOT+url,
    data:option.param,
    dataType:'json',
    type:'post',
    processData: false,
    contentType: false,
    beforeSend:function(){
      // commit('setGlobalLoading',true);
    },
    success: function (data) {
      toLogin(data);
      if (data.result == "true"){
        if (option.success && typeof option.success == 'function'){
          option.success(data,option.extra)
        }
        if (success && typeof success == 'function'){
          success(data);
        }
      }else{
        if (data.resultDesc){
          Message.error(data.resultDesc);
        }
      }
    },
    error: function () {
      Message.error('request error');
    },
    complete:function(){
      // commit('setGlobalLoading',false);
    }
  })
};

function toLogin(data){
  if(data && data.retCode == '104'){
    var loginUrl = data.data.split('?')[0] /*+ '?service='+encodeURIComponent(window.location.href);*/
    Message.warning('login timeout');
    console.log(loginUrl)
    debugger
    setTimeout(function () {
      window.location.href = loginUrl;
    },1500)
  }
}
export default {
  apiPost,
  apiGet,
  apiUpload
}


